﻿using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace ProjectMigrator
{
    public partial class CodeConversion
    {
        private ConversionItemCollection ConversionItems
        {
            get { return PopulateConversionItems(); }
        }

        public CodeConversion()
        {
        }

        public void Run()
        {
        }

        public string Convert(string contents)
        {
            if (string.IsNullOrEmpty(contents))
            {
                return contents;
            }

            foreach (var item in ConversionItems)
            {
                if (item.IsRegEx)
                {
                    contents = Regex.Replace(contents, item.Find, item.Replace);
                }
                else
                {
                    // TODO: This regex does not seem to work properly in certain cases.
                    contents = !item.WholeWord 
                        ? contents.Replace(item.Find, item.Replace) 
                        : Regex.Replace(contents, @"(?:(?<=^|\s)(?=\S)|(?<=\S|^)(?=\s))" + Regex.Escape(item.Find) + @"(?:(?<=\S)(?=\s|$)|(?<=\s)(?=\S|$))", item.Replace);
                }
            }

            return contents;
        }
    }
}
